﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace DataGrid.Controls
{
    public class DataGridViewProgressImageCell : DataGridViewImageCell
    {
        // Used to make custom cell consistent with a DataGridViewImageCell  用于使自定义单元格与DataGridViewImageCell保持一致
        static Image emptyImage;
        static DataGridViewProgressImageCell()
        {
            emptyImage = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        }
        public DataGridViewProgressImageCell()
        {
            this.ValueType = typeof(int);
        }
        // Method required to make the Progress Cell consistent with the default Image Cell.  方法，以使进度单元格与默认图像单元格保持一致。
        // The default Image Cell assumes an Image as a value, although the value of the Progress Cell is an int. 默认的图像单元格假设图像是一个值，尽管Progress单元格的值是int。
        protected override object GetFormattedValue(object value,
                            int rowIndex, ref DataGridViewCellStyle cellStyle,
                            TypeConverter valueTypeConverter,
                            TypeConverter formattedValueTypeConverter,
                            DataGridViewDataErrorContexts context)
        {
            return emptyImage;
        }
        /// <summary>
        /// 对单元格的重绘事件进行的方法重写。
        /// </summary>
        protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            float progressVal;
            if (value != null)
                progressVal = Convert.ToSingle(value);
            else
                progressVal = 10;


            float percentage = ((float)progressVal / 100.0f); // Need to convert to float before division; otherwise C# returns int which is 0 for anything but 100%.  除法前需要转换为浮点数;否则，c#将返回int值，该值对于除100%以外的任何值都为0。
            Brush backColorBrush = new SolidBrush(cellStyle.BackColor);
            Brush foreColorBrush = new SolidBrush(cellStyle.ForeColor);
            // Draws the cell grid 绘制单元格
            base.Paint(g, clipBounds, cellBounds,
             rowIndex, cellState, value, formattedValue, errorText,
             cellStyle, advancedBorderStyle, (paintParts & ~DataGridViewPaintParts.ContentForeground));

            StringFormat StringFormat = new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center };
            RectangleF Rectangle = new RectangleF(cellBounds.X, cellBounds.Y, cellBounds.Width, cellBounds.Height);
            if (percentage >= 0.0)
            {
                // Draw the progress bar and the text  绘制进度条和文本
                g.FillRectangle(new SolidBrush(Color.FromArgb(255, 189, 242)), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 4)), cellBounds.Height - 4);
                g.DrawString(progressVal.ToString() + "%", cellStyle.Font, foreColorBrush, Rectangle, StringFormat);
            }
            else
            {
                // draw the text 绘制文本

                if (this.DataGridView.CurrentRow.Index == rowIndex)
                    g.DrawString(progressVal.ToString("#0.0") + "%", cellStyle.Font, new SolidBrush(cellStyle.SelectionForeColor), Rectangle, StringFormat);
                else
                    g.DrawString(progressVal.ToString("#0.0") + "%", cellStyle.Font, foreColorBrush, Rectangle, StringFormat);
            }
        }
    }
}
